<?php

namespace plugin\ycMassage\app\api\controller;

use app\common\service\UploadService;
use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageOrdersSub;
use plugin\ycMassage\utils\enum\OrdersState;
use plugin\ycMassage\utils\enum\OrdersType;
use support\Request;

class OrdersSubController extends BaseController
{
    public function list(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 10);
        $uid = $request->uid;
        $where = [
            ['os.saas_appid', '=', $request->saas_appid],
            ['o.orders_type', '=', OrdersType::RESERVATION['value']]
        ];
        $where[] = ['os.uid', '=', $uid];
        $state = $request->get('state', 'all');
        if ($state != 'all') {
            foreach (OrdersState::getData() as $key => $value) {
                if ($state == $key) {
                    $where[] = ['os.state', '=', $value['value']];
                }
            }
        }
        $filed = [
            'o.trade,o.remark',
            'os.*',
            'address.name,address.mobile',
            'ut.nickname as technician_nickname,ut.headimg as technician_headimg',
            'uas.start_time,uas.duration',
            's.title as service_title,s.thumb,s.tips',
        ];
        $MassageOrdersSubObj = MassageOrdersSub::alias('os')->where($where);
        $technician = $request->get('technician');
        if ($technician) {
            $where[] = ['t.name|t.mobile', 'like', "%{$technician}%"];
            $MassageOrdersSubObj->join('MassageUserTechnician t', 't.uid=os.technician_id');
        }
        $MassageOrdersSubObj->join('MassageUser ut', 'ut.id=os.technician_id');
        $name = $request->get('name');
        if ($name) {
            $where[] = ['address.name', 'like', "%{$name}%"];
        }
        $mobile = $request->get('mobile');
        if ($mobile) {
            $where[] = ['address.mobile', 'like', "%{$mobile}%"];
        }
        $MassageOrdersSubObj->join('MassageOrders o', 'o.id = os.orders_id');
        $MassageOrdersSubObj->join('MassageOrdersAddress address', 'address.orders_id = os.orders_id');
        $MassageOrdersSubObj->join('MassageService s', 's.id = os.service_id');
        $MassageOrdersSubObj->join('MassageAppointmentService uas', 'uas.orders_sub_id = os.id', 'left');
        $data = $MassageOrdersSubObj->order('os.id desc')
            ->field($filed)
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ])->each(function ($item) {
                if ($item->technician_nickname) {
                    $item->technician_nickname = base64_decode($item->technician_nickname);
                }
                $item->state_text = OrdersState::getText($item->state);
                if ($item->thumb) {
                    $item->thumb = UploadService::url($item->thumb);
                }
            });
        return $this->successRes($data);
    }
}
